package com.david.equipment.service.impl;

import com.david.equipment.mapper.FieldMapper;
import com.david.equipment.pojo.entity.FieldEntity;
import com.david.equipment.service.FieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FieldServiceImpl implements FieldService {
    @Autowired
    private FieldMapper fieldMapper;
    @Override
    public List<FieldEntity> getFields(FieldEntity field) {
        return fieldMapper.selectFieldsAll(field);
    }

    @Override
    @Transactional
    public Boolean addField(String name, String type) {
        List<FieldEntity> fieldEntityList = fieldMapper.selectField(name,type);
        if (!fieldEntityList.isEmpty()) return false;
        fieldMapper.insertField(name,type);
        return true;
    }

    @Override
    public Boolean editField(FieldEntity field) {
        List<FieldEntity> fieldEntityList = fieldMapper.selectField(field.getName(),field.getType());
        if (!fieldEntityList.isEmpty()) return false;
        fieldMapper.updateField(field);
        return true;
    }

    @Override
    public void delField(Integer id) {
        fieldMapper.deleteField(id);
    }
}
